const fs = require("fs");
const path = require("path");
const ejs = require("ejs");

class GeneratorTool {
  constructor() {
    this.templatesDir = path.join(__dirname, "templates");
    this.routesDir = path.join(__dirname, "../../routes");
    this.controllersDir = path.join(__dirname, "../../controllers");
    this.servicesDir = path.join(__dirname, "../../services");
    this.modelsDir = path.join(__dirname, "../../models");
    this.migrationsDir = path.join(__dirname, "../../migrations");
  }

  async generateCode(paramsOptions) {
    const { tableName, modelName, fields, title, routerPath } = paramsOptions;

    console.log(this.templatesDir);
    console.log(this.routesDir);
    console.log(this.controllersDir);
    console.log(this.servicesDir);
    console.log(this.modelsDir);
    console.log(this.migrationsDir);

    // 确保目录存在，不存在则创建
    if (!fs.existsSync(this.routesDir + "/" + routerPath)) {
      fs.mkdirSync(this.routesDir + "/" + routerPath, { recursive: true });
    }
    // 确保目录存在，不存在则创建
    if (!fs.existsSync(this.controllersDir + "/" + routerPath)) {
      fs.mkdirSync(this.controllersDir + "/" + routerPath, { recursive: true });
    }
    // 确保目录存在，不存在则创建
    if (!fs.existsSync(this.servicesDir + "/" + routerPath)) {
      fs.mkdirSync(this.servicesDir + "/" + routerPath, { recursive: true });
    }

    // 渲染模板并生成文件
    const filesToGenerate = [
      "Router",
      "Controller",
      "Service",
      "Models",
      "Migrations",
    ];
    for (const fileType of filesToGenerate) {
      const templatePath = path.join(this.templatesDir, `${fileType}.ejs`);
      let outputPath = "";
      if (fileType === "Router") {
        outputPath = path.join(
          this.routesDir + "/" + routerPath + "/",
          `${tableName}.js`,
        );
      }

      if (fileType === "Controller") {
        outputPath = path.join(
          this.controllersDir + "/" + routerPath + "/",
          `${tableName}${fileType}.js`,
        );
      }

      if (fileType === "Service") {
        outputPath = path.join(
          this.servicesDir + "/" + routerPath + "/",
          `${tableName}${fileType}.js`,
        );
      }

      if (fileType === "Models") {
        outputPath = path.join(
          this.modelsDir + "/",
          `${modelName.toLowerCase()}.js`,
        );
      }

      if (fileType === "Migrations") {
        const date = new Date();
        const timestamp = `${date.getFullYear()}${("0" + (date.getMonth() + 1)).slice(-2)}${("0" + date.getDate()).slice(-2)}${("0" + date.getHours()).slice(-2)}${("0" + date.getMinutes()).slice(-2)}${("0" + date.getSeconds()).slice(-2)}`;
        outputPath = path.join(
          this.migrationsDir + "/",
          `${timestamp}-create-${modelName.toLowerCase()}.js`,
        );
      }

      try {
        const renderedTemplate = await ejs.renderFile(templatePath, {
          tableName,
          modelName,
          fields,
          title,
          routerPath,
        });
        fs.writeFileSync(outputPath, renderedTemplate);
        console.log(`${fileType} for ${modelName} generated successfully.`);
      } catch (error) {
        console.error(`Error generating ${fileType} for ${modelName}:`, error);
      }
    }
  }
}

module.exports = GeneratorTool;
